﻿import QtQuick 2.11
import QtGraphicalEffects 1.0

import ".."
import "../_Global"
import "../_Global/Progress"
import "../_Global/Popup"

import "../_Global/Global.js" as GlobalJS
import "../GameJS.js" as GameJS

Popup {
    //用户选择信号;-1:超时，0及以上是选择
    signal choiced()

    function showContinualChoiceWordMove(continualTimes, bContinue) {

        continualChoiceWordMove.textAnimation.moveAniX.to = continualChoiceWordMove.x;
        continualChoiceWordMove.textAnimation.moveAniY.to = Global.dpH(0);
        //continualChoiceWordMove.x = Global.dpW(250);
        continualChoiceWordMove.y = Global.dpH(200);
        continualChoiceWordMove.textAnimation.opacityAni.from = 1;
        continualChoiceWordMove.textAnimation.opacityAni.to = 0;
        continualChoiceWordMove.textAnimation.opacityAni.duration = 1500;

        if(bContinue == true) {
            if(continualTimes > 1)
            {
                continualChoiceWordMove.text.text = "连击+" + continualTimes
                continualChoiceWordMove.textAnimation.start();
            }
        }
        else {
            continualChoiceWordMove.text.text = "加分:" + continualTimes
            continualChoiceWordMove.textAnimation.start();
        }
    }

    function showFastChoiceWordMove() {
        fastChoiceWordMove.textAnimation.moveAniX.to = fastChoiceWordMove.x;
        fastChoiceWordMove.textAnimation.moveAniY.to = Global.dpH(0);
        //fastChoiceWordMove.x = Global.dpW(20);
        fastChoiceWordMove.y = Global.dpH(200);
        fastChoiceWordMove.textAnimation.opacityAni.from = 1;
        fastChoiceWordMove.textAnimation.opacityAni.to = 0;
        fastChoiceWordMove.textAnimation.opacityAni.duration = 1500;


        fastChoiceWordMove.textAnimation.start();

    }

    id: root
    width: 264
    height: 293
    color: "#EEFFEE"
    visible: false
    enabled: true


    Rectangle {
        id: rectBack
        anchors.fill: parent
        color: "#00000000"

        //这里是边框效果
        RectangularGlow {
            id: rectGlow
            visible: true
            anchors.fill: parent
            glowRadius: 18  //边框宽度
            spread: 0.1    //虚化程度(0~1)
            color: "red"  //颜色

            cornerRadius: parent.radius + glowRadius
        }

        Image { //背景
            id: backImage
            source: Global.resource.images.gameGame.wordBack
            anchors.fill: parent
        }

    }


    Text {
        id: wordName
        text: qsTr("text")
        color: "white"
        font.pixelSize: Global.gamePos.mapQuestionBox.nFontWord
        //font.family: Global.resource.fonts.font1.name
        font.bold: true
        //style: Text.Outline
        //styleColor: "lightgreen"
        x: Global.gamePos.mapQuestionBox.rectWord.x
        y: Global.gamePos.mapQuestionBox.rectWord.y
        width: Global.gamePos.mapQuestionBox.rectWord.width
        height: Global.gamePos.mapQuestionBox.rectWord.height
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        MouseArea {
            anchors.fill: parent
            onClicked: GameCore.readWord(wordName.text);
        }

        //anchors.horizontalCenter: parent.horizontalCenter
    }


    Progress {
        id: progress
        //x: Global.gamePos.mapQuestionBox.rectProgress.x
        y: Global.gamePos.mapQuestionBox.rectProgress.y
        width: Global.gamePos.mapQuestionBox.rectProgress.width
        height: Global.gamePos.mapQuestionBox.rectProgress.height
        anchors.horizontalCenter: parent.horizontalCenter
        //width: 100
        //height: 10
        imageProgressBack {
            source: Global._FixResourcePath_R("Media/Images/Game/Progress-0.png");
            /*border.left:
                switch(Global.resolution) {
                    case 1:
                        return 10;
                    case 2:
                        return 10;
                }
            border.right:
                switch(Global.resolution) {
                    case 1:
                        return 10;
                    case 2:
                        return 10;
                }*/
        }
        imageProgressFront {
            source: Global._FixResourcePath_R("Media/Images/Game/Progress-1.png");
            /*border.left:
                switch(Global.resolution) {
                    case 1:
                        return 10;
                    case 2:
                        return 10;
                }
            border.right:
                switch(Global.resolution) {
                    case 1:
                        return 10;
                    case 2:
                        return 10;
                }*/
        }
        onTimeUp: {
            _private.choiceAnswer(-1)
        }
    }


    WordMove {
        id: continualChoiceWordMove

        width: text.contentWidth
        anchors.right: parent.right
        anchors.rightMargin: Global.dpW(20)

        text {
            id: continualText
            text: ""
            font {
                //family: Global.resource.fonts.font1.name
                pixelSize:
                    switch(Global.resolution) {
                        case 1:
                            return Global.dpH(25);
                        case 2:
                            return Global.dpH(35)
                    }
            }
            style: Text.Outline
            styleColor: "white"
            color: "red"
        }
        opacity: 0
    }

    WordMove {
        id: fastChoiceWordMove
        anchors.left: parent.left
        anchors.leftMargin: Global.dpW(20)

        text {
            text: "神速"
            font {
                //family: Global.resource.fonts.font1.name
                pixelSize:
                    switch(Global.resolution) {
                        case 1:
                            return Global.dpH(25);
                        case 2:
                            return Global.dpH(35)
                    }
            }
            style: Text.Outline
            styleColor: "white"
            color: "red"
        }
        opacity: 0
    }

    Component {
        id: answerBox

        Rectangle {
            property alias explainText: explainText
            property int answer
            width: Global.gamePos.mapQuestionBox.sizeAnswer.width
            height: Global.gamePos.mapQuestionBox.sizeAnswer.height
            //y: 100
            //anchors.left: parent.left
            //anchors.right: parent.right
            //anchors.leftMargin: 5
            //anchors.rightMargin: 5
            //anchors.topMargin: 2

            color: "white"
            radius: 3

            Text {
                id: explainText
                font.pixelSize: Global.gamePos.mapQuestionBox.nFontAnswer
                //font.family: Global.resource.fonts.font1.name
                //font.bold: true
                //style: Text.Outline
                //styleColor: "white"
                color: "black"
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
                //text: qsTr("1")
            }
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                enabled: parent.enabled
                onEntered: {
                    if(enabled)parent.color = "yellow"
                }
                onExited:  {
                    if(enabled)parent.color = "white"
                }
                onPressed: {
                    if(enabled)parent.color = "red"
                }
                onReleased: {
                    if(enabled)parent.color = "white"
                }
                onClicked: {
                    _private.choiceAnswer(answer)
                }

                onEnabledChanged: {
                    //parent.color = "white"

                }
            }
        }
    }

    function hideQuestion() { //显示问题,对外
        root.hide();
        if(progress.running)
            progress.stop();
    }

    function showQuestion(w,type,time) { //显示问题,对外
        var i;
        _private.pWord = w;
        wordName.text = _private.pWord.word;
        //4个答案
        for(i = 0; i < 4; i++) {
            //console.debug(word.explain[i])
            //console.debug(i,_private.explains[i],_private.explains[i].explainText)
            _private.explains[i].explainText.text = _private.pWord.explain[i];
        }


        if(type == 0) {    //显示选择结果

            for(i = 0; i < 4; i++) {
                _private.explains[i].enabled = false;
            }

            //buttonOK.enabled = false;
            time = (time > 0 ? time : Global.config.nQuestionShowDelay);
            _private.initChoices(type);
            rectGlow.visible = false;
        }
        else if(type == 1) { //玩家选择

            GameCore.readWord(wordName.text);

            for(i = 0; i < 4; i++) {
                _private.explains[i].enabled = true;
            }

            time = (time > 0 ? time : Global.config.nQuestionChoiceDelay);
            _private.initChoices(type);
            rectGlow.visible = true;
            if(Global.config.bEffectOn)Global.resource.effects.effectQuestion.play();
        }
        else if(type == 2) {    //电脑选择

            GameCore.readWord(wordName.text);

            for(i = 0; i < 4; i++) {
                _private.explains[i].enabled = false;
            }

            //buttonOK.enabled = false;
            time = (time > 0 ? time : Global.config.nComQuestionChoiceDelay);
            _private.initChoices(type);
            rectGlow.visible = false;
        }
        else if(type == 3) {    //其他玩家选择

            GameCore.readWord(wordName.text);

            for(i = 0; i < 4; i++) {
                _private.explains[i].enabled = false;
            }

            //buttonOK.enabled = false;
            time = (time > 0 ? time : Global.config.nQuestionChoiceDelay);
            _private.initChoices(type);
            rectGlow.visible = false;
        }
        else
            console.error("显示问题出错!!!");

        if(progress.running) {
            progress.stop();
            //root.enabled = false;
            //root.hide();
        }

        root.show();
        //root.enabled = true;
        //root.visible = true
        progress.start(time,false);
    }


    //onClosing: {
    //    close.accepted = false
    //}

    Component.onCompleted: {
        //word = {"word":"word","ph":"ph","answers":["a","b","c","d"],"iAnswer":1}
        //show(0)
        for(var i = 0; i < 4; i++) {
            var tmpObj = answerBox.createObject(root,{
                                                    //"x": Qt.binding(function(){return Global.gamePos.mapQuestionBox.pointAnswer[i].x;})
                                                    //, "y": Qt.binding(function(){return Global.gamePos.mapQuestionBox.pointAnswer[i].y;})
                                                });
            _private.explains.push(tmpObj);
            _private.explains[i].answer = i;
            if(i == 0) {
                //_private.explains[i].anchors.top = wordName.bottom;
                //_private.explains[i].anchors.topMargin = 50;
            }
            else {
                //_private.explains[i].anchors.top = _private.explains[i-1].bottom;
                ////_private.explains[i].anchors.topMargin = 10;
            }
            _private.tmpBind(i,tmpObj);

        }

        //root.setAttribute(Qt.WA_TranslucentBackground);
        //root.setStyleSheet("background:transparent;");

    }

    QtObject {
        id: _private

        property QtObject pWord     //对应C++的_WordObject
        property var explains: []   //4个解释对象
        property bool bFlagCorrectAnswer: UserInfo.flagCorrectAnswer //是否标注正确答案
        property color rightColor: "#A000FF00"
        property color errColor: "#A0FF0000"
        property color normalColor: "#FFFFFFFF"

        function choiceAnswer(answer) {
            progress.stop();
            //root.enabled = false;
            root.hide();
            //root.visible = false

            //选择的答案和时间比直接保存到C++的_WordObject中!
            if(answer >= 0) {
                pWord.answerChoice = answer;
                pWord.choiceTime = (1 - progress.imageProgressFront.width
                        / progress.imageProgressBack.width)
                        * progress.numberAni.duration;   //求已用比例,乘以总时间,已用时间
                //console.debug("!!!比:", Global.config.nQuestionChoiceDelay, Global.config.nQuestionShowDelay, progress.imageProgressFront.width, progress.imageProgressBack.width, progress.imageProgressFront.width / progress.imageProgressBack.width, pWord.choiceTime);
            }
            choiced();
        }

        function initChoices(type) {    //初始化

            for(var i = 0; i < 4; i++) {
                if(type == 0) { //显示答案
                    if(i == pWord.answerChoice) {   //如果是玩家选择的
                        if(i == pWord.answer) {     //如果是正确答案
                            if(Global.config.bEffectOn)Global.resource.effects.effectCorrect.play();
                            explains[i].color = rightColor;
                        }
                        else {
                            if(Global.config.bEffectOn)Global.resource.effects.effectMistake.play();
                            explains[i].color = errColor;
                        }
                    }
                    else if(i == pWord.answer && bFlagCorrectAnswer) {//如果是正确答案
                        explains[i].color = rightColor;
                    }
                    else explains[i].color = normalColor;//没有选择

                }
                else {
                    explains[i].color = normalColor;
                }
            }
            /*
            if(showType != 0)radio0.checked = false;
            else radio0.checked = true;

            if(showType != 1)radio1.checked = false;
            else radio1.checked = true;

            if(showType != 2)radio2.checked = false;
            else radio2.checked = true;

            if(showType != 3)radio3.checked = false;
            else radio3.checked = true;
            */
        }

        function tmpBind(i,tmpObj) {
            tmpObj.x = Qt.binding(function(){return Global.gamePos.mapQuestionBox.pointAnswer[i].x;})
            tmpObj.y = Qt.binding(function(){return Global.gamePos.mapQuestionBox.pointAnswer[i].y;})
        }
    }

}
